#include "camera.h"

camera* camera::instance = NULL;

camera* camera::getInstance(){
	if(!instance){
		instance = new camera();
	}
	return instance;
}

camera::camera(){
	cameraPos.x = 0.0;
	cameraPos.y = 208.0-96.0;
}

camera::~camera(){
}

void camera::setPos(tFloat posX,tFloat posY){
	cameraPos.x = posX;
	cameraPos.y = posY;
	BG0_X0 = (int)posX;
	BG0_Y0 = (int)posY;
}

t2DVector camera::updateSpritesOffset(t2DVector location,t2DVector mapSize){
	t2DVector offset;
	if(location.y < SCREEN_HEIGHT/2.0){
		if(location.x < SCREEN_WIDTH/2.0){
			offset.x = 0.0;
			offset.y = 0.0;
		}else if(location.x > mapSize.x - SCREEN_WIDTH/2.0){
			offset.x = mapSize.x - (Fixed)SCREEN_WIDTH;
			offset.y = 0.0;
		}else{
			offset.x = location.x - SCREEN_WIDTH/2.0;
			offset.y = 0.0;
		}
	}else if(location.y > mapSize.y - SCREEN_HEIGHT/2.0){
		if(location.x < SCREEN_WIDTH/2.0){
			offset.x = 0.0;
			offset.y = mapSize.y - (Fixed)SCREEN_HEIGHT;
		}else if(location.x >  mapSize.x - SCREEN_WIDTH/2.0){
			offset.x = mapSize.x - (Fixed)SCREEN_WIDTH;
			offset.y = mapSize.y - (Fixed)SCREEN_HEIGHT;
		}else{
			offset.x=location.x - SCREEN_WIDTH/2.0;
			offset.y=mapSize.y - (Fixed)SCREEN_HEIGHT;
		}
	}else{
		if(location.x < SCREEN_WIDTH/2.0){
			offset.x=0.0;
			offset.y=location.y - SCREEN_HEIGHT/2.0;
		}else if(location.x > mapSize.x-SCREEN_WIDTH/2.0){
			offset.x = mapSize.x - (Fixed)SCREEN_WIDTH;
			offset.y=location.y - SCREEN_HEIGHT/2.0;
		}else{
			offset.x=location.x - SCREEN_WIDTH/2.0;
			offset.y=location.y - SCREEN_HEIGHT/2.0;
		}
	}
	return offset;
}

t2DVector camera::getPos(){
	return cameraPos;
}